package com.samp.solr.hanlp.syntax;


import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord;

/**
 * 依存句法分析,注意设置：-Xms1g -Xmx1g -Xmn512m
 */
public class DependencyParserDemo {

    private static void demo1(String msg){
        CoNLLSentence sentence = HanLP.parseDependency(msg);
        System.out.println(sentence);
        System.out.println("-----直接拿到数组，任意顺序或逆序遍历>>>>>>>>>>");
        CoNLLWord[] wordArray = sentence.getWordArray();
        for (int i = 0; i < wordArray.length; i++)
        {
            CoNLLWord word = wordArray[i];
            System.out.printf("%s --(%s)--> %s\n", word.LEMMA, word.DEPREL, word.HEAD.LEMMA);
        }
//        System.out.println("-----还可以直接遍历子树，从某棵子树的某个节点一路遍历到虚根（有问题）>>>>>>>>>>");
//        CoNLLWord head = wordArray[12];
//        while ((head = head.HEAD) != null)
//        {
//            if (head == CoNLLWord.ROOT) System.out.println(head.LEMMA);
//            else System.out.printf("%s --(%s)--> ", head.LEMMA, head.DEPREL);
//        }
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        try{
            String msg = "徐先生还具体帮助他确定了把画雄鹰、松鼠和麻雀作为主攻目标。";
            msg = "华盛顿州副州长Cyrus Habib热情欢迎了王东华总领事一行，他对中美企业在华盛顿州展开合作充满期待，";
            demo1(msg);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("cost:"+(System.currentTimeMillis()-start));
    }
}
